# _*_ coding: UTF-8 _*_
# @Time     : 2022/5/16 10:51
# @Author   : Li Jie
# @Site     : http://www.hzdlstudy.com/
# @File     : App的启动与关闭.py
# @Software : PyCharm

import unittest
from appium import webdriver
import time

from selenium.webdriver.common.by import By


class CalculatorTest(unittest.TestCase):
    """
    Appium的元素定位方式：
    1.id
    2.xpath
    3.accessbility_id
    """

    def setUp(self) -> None:
        # 手机设置
        desired_caps = {}
        desired_caps['platformName'] = 'Android'  # 系统名称
        desired_caps['platformVersion'] = '7.1.2'  # 系统的版本号
        desired_caps['deviceName'] = 'Android Emulator'  # 设备名称，这里是虚拟机，这个没有严格的规定
        desired_caps['appPackage'] = 'com.example.calculator'  # APP包名
        desired_caps['appActivity'] = 'com.example.calculator.MainActivity'  # APP入口的activity
        desired_caps['noReset'] = True  # 不重置app的缓存文件
        desired_caps['unicodeKeyboard'] = True  # 设置键盘支持中文输入
        desired_caps['resetKeyboard'] = True  # 重置键盘

        # 连接appium server，告诉appium，代码要操作哪个设备上的哪个APP
        # 启动手机上App
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    def test(self):
        # 1.通过id定位：就是resouce-id
        self.driver.find_element(By.ID, 'com.example.calculator:id/button1').click()
        time.sleep(1)

        # 2.通xpath定位：通过元素在xml文件中xpath定位
        self.driver.find_element(By.XPATH,
                                 "//android.widget.Button[@resource-id='com.example.calculator:id/jia']").click()
        time.sleep(1)

        self.driver.find_element(By.ID, 'com.example.calculator:id/button2').click()
        time.sleep(1)
        self.driver.find_element(By.ID, 'com.example.calculator:id/deng').click()

        actual = self.driver.find_element(By.ID, 'com.example.calculator:id/TextView01').text

        # 断言
        self.assertEqual("3", actual)
        time.sleep(3)

    def tearDown(self) -> None:
        self.driver.quit()
